/*
 * @Author: lanbos 
 * @Date: 2017-11-24 10:36:00 
 * @Last Modified by: lanbos
 * @Last Modified time: 2017-12-18 10:28:27
 */

let util = require("./util");
let jquery = require("jquery/dist/jquery.min");
let Toast = require("cube-ui/lib/toast/toast.min");
let Vue = require("vue/dist/vue");
Vue.use(Toast);
let bus = new Vue();
function gra(min, max) {
  return ~~(Math.random() * (max - min) + min);
}
let testTime=`${gra(1990,3000)}_01_${gra(10,31)}_10_00_00`
function toast_warn(str) {
  let toast = bus.$createToast({
    time: 1500,
    txt: str,
    type: "warn"
  }).show();
}

function toast_sucess(str) {
  let toast = bus.$createToast({
    time: 1500,
    txt: str,
    type: "correct"
  }).show();
}

class BaseAjax {
  constructor(obj) {
    this.strUrl = "//match.lottery.sina.com.cn/activity/zfc_activity1203/";
    let newObj = {
      ...obj,
      format: "json",
      // _flush_cache:"1",
    }
    this.obj = newObj;
    this.type = "get";
    this.dataType = "jsonp";
    this.ajaxConf = {}
  }
  mkName(obj) {
    this.jsonpName = "";
    var self = this;
    var shadowArr = [
      "_flush_cache",
      "format",
      'ip'
    ]
    $.each(obj, function (k, v) {
      if (shadowArr.indexOf(k) < 0) {
        self.jsonpName += (v + "_")
      }
    })
  }
  runAjax(callback) {
    var self = this;
    if (this.type == 'post' || !!this.sign) {
      util.paramMd5(this.obj);
    }
    this.obj.time=testTime;
    $.ajax({
        ...self.ajaxConf,
        url: self.strUrl,
        dataType: self.dataType,
        data: self.obj,
        type: self.type,
        jsonpCallback: self.jsonpName,
        headers: {
          "UNIONID": ""
        }
      }).done(function (data) {
        if (!data.result || !data.result.status) {
          console.log(self.obj.cat1 + "服务器请求失败!");
          return
        }
        let code = data.result.status.code;
        if (1000 <= code && code <= 2000) { //常规错误，不做处理
          console.log(data);
        } else if (100 <= code && code < 1000&&code!=406) {
          toast_warn(data.result.status.msg)
        } else {
          callback(data, code);
        }
      })
      .fail(function () {
        console.log(self.obj.cat1 + "本地请求失败!")
      })
  }
  next(callback, flush = false) {
    this.runAjax(callback);
  }
}
const ajaxData = {
  // 获取用户当前ip位置
  location2(callback) {
    let aj = new BaseAjax({
      ip: '221.208.247.138',
      test: "test"
    })
    aj.strUrl += 'location2';
    aj.next((data, code) => {
      if (code == 0) {
        callback(data.result.data);
      } else {
        toast_warn(data.result.status.msg);
      }
    })
  },
  // 提交打call数据
  activity2(code, amount, callback) {
    let nickname = util.getUsr("nickname");
    nickname = util.base64Encode(nickname);
    let headimg = util.getUsr("headimgurl");
    headimg = util.base64Encode(headimg);
    let aj = new BaseAjax({
      openid: util.getUsr("openid"),
      nickname,
      sex:util.getUsr("sex"),
      headimg,
      amount,
      code
      
    })
    aj.strUrl += "activity2";
    aj.jsonpName = "activity2";
    aj.sign = 1;
    aj.next((data, code) => {
      if (code == 0) {
        callback(data.result.data);
      }
    })
  },
  // 获取用户本日打call状态
  status2(callback) {
    let aj = new BaseAjax({
      openid: util.getUsr('openid'),
    })
    aj.strUrl += "status2";
    aj.jsonpName = "status2"
    aj.next((data, code) => {
      if (code == 0||code==406) {
        callback(data.result.data,code);
      }
    })
  },
  // 用户提交绑定手机号
  savePhone2(phone, callback) {
    let aj = new BaseAjax({
      openid: util.getUsr('openid'),
      phone
    })
    aj.strUrl += "savePhone2";
    aj.jsonpName = "savePhone2";
    aj.sign = 1;
    aj.next((data, code) => {
      if (code == 0) {
        callback(data.result.data);
        
      }
    })
  },
  // 获取各省市call值排行
  ranking2(callback) {
    let aj = new BaseAjax({
    })
    aj.strUrl += "ranking2";
    aj.jsonpName = "ranking2";
    aj.next((data, code) => {
      if (code == 0) {
        callback(data.result.data);
      }
    })
  }
}
module.exports = ajaxData;